<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactStage\Service;

use MoChat\Framework\Service\AbstractService;
use MoChat\Plugin\ContactStage\Contract\ContactStageRuleContract;
use MoChat\Plugin\ContactStage\Model\ContactStageRule;

class ContactStageRuleService extends AbstractService implements ContactStageRuleContract
{
    /**
     * @var ContactStageRule
     */
    protected $model;

    /**
     * 查询单条 - 根据ID.
     * @param int $id ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactStageRuleById(int $id, array $columns = ['*']): array
    {
        return $this->model->getOneById($id, $columns);
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createContactStageRule(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id id
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateContactStageRuleById(int $id, array $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    /**
     * 查询单条 - 根据corpId.
     * @param int $corpId ID
     * @param array|string[] $columns 查询字段
     * @return array 数组
     */
    public function getContactStageRuleByCorpId(int $corpId, array $columns = ['*']): array
    {
        $res = $this->model::query()
            ->where('corp_id', $corpId)
            ->first($columns);
        if (empty($res)) {
            return [];
        }

        return $res->toArray();
    }
}
